package com.showde.shoppingmall.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.kim.androidbase.db.DBCommandInteface;
import com.kim.androidbase.db.DBSession;
import com.kim.androidbase.db.IDBUtils.QueryCompleteCallback;
import com.kim.androidbase.db.SQLiteCriteria;
import com.showde.shoppingmall.db.constant.UserConstant;
import com.showde.shoppingmall.domain.User;

public class UserDBCommand implements DBCommandInteface<User, String> {
	
	private static final String TAG = "UserDBCommand";

	DBSession dbSession;

	public UserDBCommand(Context context) {
		dbSession = new DBSession(context, new DBHelper(context));
	}

	@Override
	public void add(User t) {
		dbSession.insert(UserConstant.TABLENAME, getContentValues(t),
				SQLiteDatabase.CONFLICT_REPLACE);
	}

	@Override
	public void add(List<User> ts) {
		List<ContentValues> contentValuesList = new ArrayList<ContentValues>();
		for (User user : ts) {
			contentValuesList.add(getContentValues(user));
		}
		dbSession.insertBatch(UserConstant.TABLENAME, contentValuesList,
				SQLiteDatabase.CONFLICT_ABORT);
	}

	@Override
	public void delete(String pk) {
		dbSession.delete(UserConstant.TABLENAME, UserConstant.USERID + "=?",
				new String[] { pk });
	}

	@Override
	public void delete(SQLiteCriteria sqLiteCriteria) {
		dbSession.delete(sqLiteCriteria.getTableName(),
				sqLiteCriteria.getWhereClause(), sqLiteCriteria.getWhereArgs());
	}

	@Override
	public void modify(SQLiteCriteria sqLiteCriteria) {
		dbSession.modify(sqLiteCriteria.getTableName(),
				sqLiteCriteria.getContentValues(),
				sqLiteCriteria.getWhereClause(), sqLiteCriteria.getWhereArgs());
	}

	@Override
	public void modify(String pk, SQLiteCriteria sqLiteCriteria) {
		dbSession.modify(sqLiteCriteria.getTableName(),
				sqLiteCriteria.getContentValues(),
				sqLiteCriteria.getWhereClause(), new String[] { pk });
	}

	@Override
	public User query(String pk) {
		final User user = new User();
		dbSession.query("select * from " + UserConstant.TABLENAME + " where "
				+ UserConstant.USERID + "=?", new String[] { pk },
				new QueryCompleteCallback() {

					@Override
					public void onQueryComplete(Cursor cursor) {
						if (cursor == null || cursor.getCount() == 0) {
							return;
						}
						if (cursor.moveToNext()) {
							setUserProperty(user, cursor);
						}
					}

					@Override
					public void onQueryError(String arg0) {

					}
				});
		return user;
	}

	public User queryCurrentUser() {
		final User user = new User();
		dbSession.query("select * from " + UserConstant.TABLENAME, null,
				new QueryCompleteCallback() {

					@Override
					public void onQueryComplete(Cursor cursor) {
						if (cursor == null || cursor.getCount() == 0) {
							return;
						}
						if (cursor.moveToNext()) {
							setUserProperty(user, cursor);
						}
					}

					@Override
					public void onQueryError(String arg0) {

					}
				});
		return user;
	}

	@Override
	public List<User> query(SQLiteCriteria sqLiteCriteria) {
		return null;
	}

	@Override
	public List<User> queryAll() {
		return null;
	}
	
	public User querUserOnLogin(String userName, String password){
		
		final User user = new User();
		
		dbSession.query(UserConstant.TABLENAME, UserConstant.dbColumns,
				UserConstant.USERNAME + "=? and " + UserConstant.USERPASSWORD
						+ "=?", new String[] { userName, password }, null,
				null, null, new QueryCompleteCallback() {
					
					@Override
					public void onQueryError(String errorMsg) {
						Log.d("TAG", "querUserOnLogin:"+errorMsg);
					}
					
					@Override
					public void onQueryComplete(Cursor cursor) {
						if(cursor != null && cursor.moveToNext()){
							setUserProperty(user, cursor);
						}
					}
				});
		
		return user.getUserID() == null ? null : user;
	}

	private void setUserProperty(User user, Cursor cursor) {
		user.setUserAccount(dbSession.getString(cursor, UserConstant.USERACCOUNT));
		user.setUserCardNum(dbSession.getString(cursor,  UserConstant.USERCARDNUM));
		user.setUserCardType(dbSession.getString(cursor, UserConstant.USERCARDTYPE));
		user.setUserCity(dbSession.getString(cursor, UserConstant.USERCITY));
		user.setUserCountry(dbSession.getString(cursor, UserConstant.USERCOUNTRY));
		user.setUserCreateTime(dbSession.getString(cursor, UserConstant.USERCREATETIME));
		user.setUserEmail(dbSession.getString(cursor, UserConstant.USEREMAIL));
		user.setUserGender(dbSession.getString(cursor, UserConstant.USERGENDER));
		user.setUserHeadIcon(dbSession.getString(cursor, UserConstant.USERHEADICON));
		user.setUserID(dbSession.getString(cursor, UserConstant.USERID));
		user.setUserMobile(dbSession.getString(cursor, UserConstant.USERMOBILE));
		user.setUserModifyTime(dbSession.getString(cursor, UserConstant.USERMODIFYTIME));
		user.setUserMSN(dbSession.getString(cursor, UserConstant.USERMSN));
		user.setUserName(dbSession.getString(cursor, UserConstant.USERNAME));
		user.setUserNickName(dbSession.getString(cursor, UserConstant.USERNICKNAME));
		user.setUserOther(dbSession.getString(cursor, UserConstant.USEROTHER));
		user.setUserPassword(dbSession.getString(cursor, UserConstant.USERPASSWORD));
		user.setUserPhone(dbSession.getString(cursor, UserConstant.USERPHONE));
		user.setUserPostcode(dbSession.getString(cursor, UserConstant.USERPOSTCODE));
		user.setUserProvince(dbSession.getString(cursor, UserConstant.USERPROVINCE));
		user.setUserQQ(dbSession.getString(cursor, UserConstant.USERQQ));
		user.setUserRemark(dbSession.getString(cursor, UserConstant.USERREMARK));
	}

	private ContentValues getContentValues(User user) {
		ContentValues contentValues = new ContentValues();
		contentValues.put(UserConstant.USERACCOUNT, user.getUserAccount());
		contentValues.put(UserConstant.USERCARDNUM, user.getUserCardNum());
		contentValues.put(UserConstant.USERCARDTYPE, user.getUserCardType());
		contentValues.put(UserConstant.USERCITY, user.getUserCity());
		contentValues.put(UserConstant.USERCOUNTRY, user.getUserCountry());
		contentValues.put(UserConstant.USERCREATETIME, user.getUserCreateTime());
		contentValues.put(UserConstant.USEREMAIL, user.getUserEmail());
		contentValues.put(UserConstant.USERGENDER, user.getUserGender());
		contentValues.put(UserConstant.USERID, user.getUserID());
		contentValues.put(UserConstant.USERMOBILE, user.getUserMobile());
		contentValues.put(UserConstant.USERMODIFYTIME, user.getUserModifyTime());
		contentValues.put(UserConstant.USERMSN, user.getUserMSN());
		contentValues.put(UserConstant.USERNAME, user.getUserName());
		contentValues.put(UserConstant.USERNICKNAME, user.getUserNickName());
		contentValues.put(UserConstant.USEROTHER, user.getUserOther());
		contentValues.put(UserConstant.USERPASSWORD, user.getUserPassword());
		contentValues.put(UserConstant.USERPHONE, user.getUserPhone());
		contentValues.put(UserConstant.USERPOSTCODE, user.getUserPostcode());
		contentValues.put(UserConstant.USERPROVINCE, user.getUserProvince());
		contentValues.put(UserConstant.USERQQ, user.getUserQQ());
		contentValues.put(UserConstant.USERREMARK, user.getUserRemark());
		return contentValues;
	}

}
